<template>
	<view class="content">
		<view class="noticer_class">
			<u-notice-bar speed=40 color=#FF6347 :text="notice[0].notice"></u-notice-bar>
		</view>
		<u-overlay :show="show" @click="show = true">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="item_box">
						-批量接码，自动注册，过实名，包养号-
					</view>
					<view class="item_box">
						---联系作者免费开通授权账号---
					</view>
					<view class="item_box">
						----------养号,每日抽成5%----------
					</view>
					<view class="item_box">
						-----请输入授权账号密码-----
					</view>
					<u-code-input @finish="fnuserId" v-model="user_id" :maxlength="6"></u-code-input>
					<u-code-input @finish="fnpassword" v-model="password" :maxlength="6"></u-code-input>
					<u-button color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))" text="取消"
						@click="cancelLogin"></u-button>
				</view>
			</view>
		</u-overlay>
		<view class="head_box_class">
			<view class="user_info">
				<!-- 			<image src="https://oss.51dedia.com/avatar.png" mode="" class="win_image"></image>
				<view>
					接码平台id:{{em_user_id}}
				</view> -->
				<image src="https://oss.51dedia.com/avatar.png" mode="" class="win_image"></image>
				<view>
					接码平台余额:{{pearl}}
				</view>
			</view>

			<view class="user_info">

				<image src="https://oss.51dedia.com/avatar.png" mode="" class="win_image"></image>
				<view>
					管理:{{userList.length}}
				</view>
			</view>

			<view class="user_info">

				<image src="https://oss.51dedia.com/avatar.png" mode="" class="win_image"></image>
				<view>
					今日养号应付（管理*0.05）:{{paySum}}
				</view>
			</view>
		</view>

		<u-alert title="点击步进器的数字可自定义注册数量" type="warning" effect="dark"></u-alert>
		<u-number-box button-size="40" v-model="size" integer min="1" max="100"></u-number-box>
		<u-button v-if="!login_flag" color="linear-gradient(to right, rgb(90, 240, 216), rgb(213, 80, 250))"
			text="①登录作者授权账号" @click="loginFlag"></u-button>
		<u-button v-if="!is_login" color="linear-gradient(to right, rgb(90, 83, 216), rgb(213, 51, 100))" text="登录接码平台"
			@click="login"></u-button>
		<u-button color="linear-gradient(to right, rgb(51, 100, 10), rgb(213, 51, 150))" text="一键转猪指定ID"
			@click="autoTransfer"></u-button>

		<u-button color="linear-gradient(to right, rgb(100, 83, 216), rgb(213, 51, 10))" text="选择文件批量导入"
			@click="selectFile"></u-button>
		<u-button color="linear-gradient(to right, rgb(213, 83, 100), rgb(51, 51, 100))" text="一键上阵"
			@click="startAppear"></u-button>

		<u-button v-if="is_login" color="linear-gradient(to right, rgb(213, 83, 100), rgb(180, 51, 186))" text="开始一键注册"
			@click="startRegister"></u-button>

		<u-button color="linear-gradient(to right, rgb(40, 83, 180	), rgb(200, 51, 100))" text="实时查看日志"
			@click="realTimeLog"></u-button>

		<!-- 账号展示区 -->
		<view class="account_class">
			<view class="account_box">

				<view class="item_list1">
					ID
				</view>
				<view class="item_list1">
					号码
				</view>
				<view class="item_list">
					珍珠
				</view>
				<view class="item_list1">
					密码
				</view>
				<view class="item_list2">
					时间
				</view>
			</view>
		</view>
		<view class="account_class">
			<view class="account_box" v-for="item,index in userList">
				<view class="item_list1">
					{{item.user_id}}
				</view>
				<view class="item_list1">
					{{item.mobile}}
				</view>
				<view class="item_list">
					{{item.pearl}}
				</view>
				<view class="item_list1">
					{{item.password}}
				</view>
				<view class="item_list2">
					{{item.create_time.substring(5)}}
				</view>
			</view>
		</view>

		<u-modal :title="transfermsg" :show="transfer_show" closeOnClickOverlay showCancelButton @confirm="confirm1"
			@cancel="cancel">
			<image style="width: 150rpx;height: 150rpx; border-radius: 30rpx;" :src="avatar_src"></image>
		</u-modal>


		<u-modal :title="transfermsg1" :show="transfer_show1" closeOnClickOverlay showCancelButton @confirm="confirm2"
			@cancel="cancel1">
			<image style="width: 150rpx;height: 150rpx; border-radius: 30rpx;" :src="avatar_src1"></image>
		</u-modal>
	</view>
</template>

<script>
	import url from "../../js/url.js"
	export default {
		data() {
			return {
				user_id_flag: false,
				password_flag: false,
				//登录密码
				user_id: "",
				password: "",
				notice: null,
				token: null,
				avatar: "/static/user.png",
				secret: null,
				pearl: null,
				em_user_id: null,
				is_login: false,
				userId: null,
				size: 1,
				show: false,
				isUser: false,
				userList: [],
				transfer_show: false,
				transfer_show1: false,
				transfermsg: null,
				transfermsg1: null,
				avatar_src: null,
				avatar_src1: null,
				transferId: null,
				supperId: null,
				isUserLogin: false,
				paySum: 0,
				login_flag :false,
			}
		},
		onLoad() {
			console.log("加载")
			this.getNotice()
			//取出token
			this.token = uni.getStorageSync('token')

		},
		methods: {
			loginFlag() {
				this.show = true
			},
			realTimeLog() {
				if(!this.login_flag){
					this.show = true
					return
				}
				uni.showToast({
					title: "待开发",
					duration: 2000
				})
			},
			getAutoRegister() {
				uni.request({
					url: 'http://' + url + '/autoregister/registers?user_id=' + this.user_id + "&password=" + this
						.password,
					header: {
						//自定义请求头信息
						'authentication': this.token
					},
					method: "GET",
					success: (res) => {
						if (res.data.code == 1) {
							this.show = false
							//登录成功
							this.userList = res.data.data
						}
						//失败
						if (res.data.code == 0) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000,
								icon: "error"
							});
						}
					}
				});
			},
			startAppear() {
				if(!this.login_flag){
					this.show = true
					return
				}
				console.log("开始一键上阵")
				uni.showModal({
					title: '开始一键上阵',
					content: '开始后请勿重复提交',
					confirmText: '确认',
					cancelText: '取消',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'http://' + url + '/autoregister/appear',
								header: {
									//自定义请求头信息
									'authentication': this.token
								},
								data: {
									user_id: this.user_id,
									password: this.password
								},
								method: "POST",
								success: (res) => {
									//成功
									if (res.data.code == 1) {
										uni.showToast({
											title: res.data.msg,
											duration: 2000
										});
									}
									//失败
									if (res.data.code == 0) {
										uni.showToast({
											title: res.data.msg,
											duration: 2000,
											icon: "error"
										});
									}
								}
							});
						}
					}
				});

			},
			confirm1() {
				var that = this;
				//发送转账请求
				this.transfer_show = false
				uni.request({
					url: 'http://' + url + '/autoregister/transfer',
					header: {
						//自定义请求头信息
						'authentication': that.token
					},
					data: {
						transfer_user_id: that.transferId,
						user_id: that.user_id,
						password: that.password
					},
					method: "POST",
					success: (res) => {
						//成功
						if (res.data.code == 1) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							});
						}
						//失败
						if (res.data.code == 0) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000,
								icon: "error"
							});
						}
					}
				});

			},
			cancel() {
				this.transferId = ""
				this.transfer_show = false
			},

			confirm2() {
				var that = this;
				this.transfer_show1 = false
				uni.request({
					url: 'http://' + url + '/autoregister/register2',
					header: {
						//自定义请求头信息
						'authentication': that.token
					},
					data: {
						size: that.size,
						supperId: that.supperId,
						user_id: that.user_id,
						em_user_id: that.em_user_id,
						password: that.password
					},
					method: "POST",
					success: (res) => {
						//成功
						if (res.data.code == 1) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							});
						}
						//失败
						if (res.data.code == 0) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000,
								icon: "error"
							});
						}
					}
				});

			},
			cancel1() {
				this.supperId = ""
				this.transfer_show1 = false
			},
			autoTransfer() {
				if(!this.login_flag){
					this.show = true
					return
				}
				//进行一键转珠子
				//发送请求，
				var that = this;
				uni.showModal({
					title: '输入要转账的卡帕ID',
					content: '',
					editable: true, //是否显示输入框
					placeholderText: 'ID', //输入框提示内容
					confirmText: '确认',
					cancelText: '取消',
					success: (res) => {
						that.transferId = res.content
						if (res.confirm) {
							uni.request({
								url: 'http://' + url + '/autoregister/userinfo?user_id=' + that
									.transferId,
								header: {
									//自定义请求头信息
									'authentication': this.token
								},
								method: "GET",
								success: (res) => {
									this.transfer_show = true
									this.transfermsg = "转账至:" + res.data.data.data.info.screen_name
									this.avatar_src = res.data.data.data.info.avatar
								}
							});
						}
					}
				});
			},
			selectFile() {
				if(!this.login_flag){
					this.show = true
					return
				}
				//选择文件上传
				let that = this;
				uni.chooseFile({
					count: 1, //默认100
					extension: ['.txt'],
					success: function(res) {
						var tempFilePaths = res.tempFilePaths
						uni.showModal({
							title: '批量导入',
							content: "导入格式确保符合 账号----密码 格式",
							success: function(res) {
								//将文件上传到服务器
								if (res.confirm) {
									uni.uploadFile({
										url: "http://" + url +
											"/autoregister/upload", //仅为示例，非真实的接口地址
										filePath: tempFilePaths[0],
										name: 'file',
										header: {
											//自定义请求头信息
											'authentication': that.token
										},
										success: (uploadFileRes) => {
											//响应结果
											//成功
											if (res.data.code == 1) {
												uni.showToast({
													title: res.data.msg,
													duration: 2000
												});
											}
											//失败
											if (res.data.code == 0) {
												uni.showToast({
													title: res.data.msg,
													duration: 2000,
													icon: "error"
												});
											}
										}
									});
								}
							}

						})

					}
				});
			},

			startRegister() {
				if(!this.login_flag){
					this.show = true
					return
				}
				//开始一键注册、
				var that = this
				uni.showModal({
					title: '输入要作为上级的卡帕ID',
					content: '',
					editable: true, //是否显示输入框
					placeholderText: 'ID', //输入框提示内容
					confirmText: '确认',
					cancelText: '取消',
					success: function(res) {
						if (res.confirm) {
							that.supperId = res.content;
							uni.request({
								url: 'http://' + url + '/autoregister/userinfo?user_id=' + that
									.supperId,
								header: {
									//自定义请求头信息
									'authentication': that.token
								},
								method: "GET",
								success: (res) => {
									that.transfer_show1 = true
									that.transfermsg1 = "上级卡帕ID：" + that.supperId + " 昵称：" + res
										.data
										.data.data.info
										.screen_name
									that.avatar_src1 = res.data.data.data.info.avatar
								}
							});

						} else if (res.cancel) {
							that.supperId = ""
						}
					}

				})
			},
			fnuserId(e) {
				this.user_id_flag = true;
				if (this.user_id_flag == true && this.password_flag == true) {
					//调用查询方法
					this.checkUser()
				}

			},

			fnpassword(e) {
				this.password_flag = true;
				if (this.user_id_flag == true && this.password_flag == true) {
					//调用查询方法
					this.checkUser()
				}
			},
			checkUser() {
				uni.showLoading({
					title: '查询中'
				});
				this.loginReq()
				//无论成功与否
				this.password_flag = false;
				this.user_id_flag = false;

			},
			cancelLogin() {
				this.show = false
			},

			loginReq() {
				//发起登录请求
				uni.request({
					url: 'http://' + url + '/autoregister/loginreq',
					header: {
						//自定义请求头信息
						'authentication': this.token
					},
					data: {
						user_id: this.user_id,
						password: this.password
					},
					method: "POST",
					success: (res) => {
						uni.hideLoading()
						if (res.data.code == 1) {
							setInterval(() => {
								this.getAutoRegister()
							}, 3000)
							this.show = false
							//登录成功
							this.userList = res.data.data
							this.paySum = this.userList.length * 0.05
							this.login_flag = true
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							});
						}
						//失败
						if (res.data.code == 0) {
							this.login_flag = false
							this.user_id = "",
								this.password = "",
								uni.showToast({
									title: res.data.msg,
									duration: 2000,
									icon: "error"
								});
						}

					}
				});
			},
			loginEma() {
				//登录接码平台请求
				uni.request({
					url: 'http://' + url + '/autoregister/login2',
					header: {
						//自定义请求头信息
						'authentication': this.token
					},
					data: {
						secret: this.secret
					},
					method: "POST",
					success: (res) => {
						uni.hideLoading()
						//成功
						if (res.data.code == 1) {
							this.show = false
							this.em_user_id = res.data.data.em_user_id
							this.pearl = res.data.data.pearl
							this.is_login = true
							uni.showToast({
								title: res.data.msg,
								duration: 2000
							});
						}
						//失败
						if (res.data.code == 0) {
							uni.showToast({
								title: res.data.msg,
								duration: 2000,
								icon: "error"
							});
						}
					}
				});
			},
			//登录接码平台
			login() {
				if(!this.login_flag){
					this.show = true
					return
				}
				uni.showModal({
					title: '接码平台账号和密码',
					content: '',
					editable: true, //是否显示输入框
					placeholderText: '账号和密码使用@分隔', //输入框提示内容
					confirmText: '确认',
					cancelText: '取消',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '登录中'
							});
							this.secret = res.content
							this.loginEma()
						}
					}
				});
			},
			getNotice() {
				//获取通知
				uni.request({
					url: 'http://' + url + '/record/notice',
					success: (res) => {
						// this.text = 'request success';
						this.notice = res.data.data
					}
				});
			}
		}
	}
</script>

<style>
	.item_list {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 10%;

	}

	.item_list2 {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 15%;
		padding-top: 15rpx;
		padding-bottom: 15rpx;
	}

	.item_list1 {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 25%;
	}

	.noticer_class {
		width: 100%;
	}

	.row_box {
		margin: 10rpx;
		width: 50%;
	}

	.butto_class {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		margin: 10rpx;
	}

	.head_box_class {
		border-radius: 20rpx;
		background-color: skyblue;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-wrap: wrap;

	}



	.account_box {
		margin: 10rpx;
		display: flex;
		flex-wrap: nowrap;
		justify-content: space-around;
		align-items: center;
		background-color: #71d5a1;
		border-radius: 15rpx;
	}

	.account_class {
		width: 100%;
	}

	.line_class {
		width: 100%;
		/* height: 100rpx; */
	}

	.item_box {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		align-items: center;
		color: red;
	}

	.rect {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: center;
		border-radius: 20rpx;
		width: 550rpx;
		height: 550rpx;
		background-color: #fff;
	}

	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
		height: 100%;
	}

	.win_image {
		width: 50rpx;
		height: 50rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
		border-radius: 10rpx;

	}

	.user_info {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
	}

	.head_class {
		margin-top: 10rpx;
		width: 50%;
	}



	.content {
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
		align-items: center;
	}
</style>